package com.jintoufs.web.action.support;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jintoufs.uuid.SnowflakeIdWorker;
import org.apache.commons.lang.StringUtils;

import com.jintoufs.domain.user.User;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

public class SupportAction {

	@Resource
	private SnowflakeIdWorker snowflakeIdWorker;



	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

	}
	/**
	 * 获得String类型请求参数
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public String getString(HttpServletRequest request, String name) {
		if (StringUtils.isNotEmpty(name)) {
			return request.getParameter(name);
		}
		return null;
	}

	/**
	 * 获得Integer类型请求参数
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public int getInteger(HttpServletRequest request, String name) {
		String value = getString(request, name);
		if (StringUtils.isNotEmpty(value)) {
			try {
				return Integer.parseInt(value);
			} catch (Exception e) {
				e.printStackTrace();
				return 0;
			}

		}
		return 0;
	}

	/**
	 * 获得Double类型请求参数
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public double getDouble(HttpServletRequest request, String name) {
		String value = getString(request, name);
		if (StringUtils.isNotEmpty(value)) {
			try {
				return Double.parseDouble(value);
			} catch (Exception e) {
				e.printStackTrace();
				return 0;
			}

		}
		return 0;
	}

	/**
	 * 获得Boolean类型请求参数
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public boolean getBoolean(HttpServletRequest request, String name) {
		String value = getString(request, name);
		if (StringUtils.isNotEmpty(value)) {
			try {
				return Boolean.getBoolean(value);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}

		}
		return false;
	}

	/**
	 * 向客户端写数据
	 * 
	 * @param response
	 * @param info
	 */
	public void writeInfo(HttpServletResponse response, String info) {
		try {
			response.setContentType("text/html;charset=UTF-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.getWriter().write(info);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void showBox(HttpServletResponse response, String context) {
		try {
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.print("<script>alert('" + context + "');history.go(-1);</script>");
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static User getCurrentUserInSession(HttpSession session) {
		return (User) session.getAttribute("user");
	}

	public static void putCurrentUserToSession(HttpSession session, User user) {
		session.setAttribute("user", user);
	}

	public String formatDate(Date date) {
		String dateStr = null;
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateStr = sdFormat.format(date);
		return dateStr;
	}

	public Date parseString(String time) throws ParseException {
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
		return sdFormat.parse(time);
	}
	public String getUUID(){
		return String.valueOf(snowflakeIdWorker.nextId());
	}
}
